var iptv = null; //全局聚焦实列
//导航栏
var nav = {
    _data: ["user", "recommend", "classify", "ranking"],
    _navIndex: 1,
    _init: function () {
        iptv.onFocus(iptv.findFocusEle(nav._navIndex, iptv._groupList.nav.foucsList, "focusIndex"));
    },
    /**
     *  导航栏 聚焦
     */
    _focus: function (eleObj) {
        var imgSrc = eleObj.ele.src.replace("_b", "_f");
        imgSrc = imgSrc.replace("_t", "_f");
        eleObj.ele.src = imgSrc;
        // iptv._enter();
    },
    /**
     *  导航栏 失焦
     */
    _blur: function (eleObj) {
        var imgSrc = eleObj.ele.src;
        if (eleObj.focusIndex == this._navIndex) {
            imgSrc = imgSrc.replace("_f", "_t");
        } else {
            imgSrc = eleObj.ele.src.replace("_f", "_b");
        }
        eleObj.ele.src = imgSrc;
    },
    //导航栏切换栏目
    _enter: function (eleObj) {
        // console.log(eleObj, 'enter');
        if (!document.getElementById(this._data[eleObj.focusIndex] + "-box")) {
            console.log('该栏目内容未定义');
            return;
        }
        var groupName = this._data[this._navIndex];
        iptv.addClass(document.getElementById(groupName + "-box"), "hidden");
        nav._navIndex = eleObj.focusIndex; //记录导航索引
        groupName = this._data[this._navIndex];
        //重置导航向下的groupName
        iptv._groupList.nav.down = groupName;
        iptv.removeClass(document.getElementById(groupName + "-box"), "hidden");
        nav.setSelectStyle(eleObj.ele);
    },
    //当前可进入/已进入的栏目样式
    setSelectStyle: function (ele) {
        var eleArray = document.getElementsByClassName('nav-img');
        for (var i = 0; i < eleArray.length; i++) {
            iptv.removeClass(eleArray[i], "nav-select");
        }
        iptv.addClass(ele, "nav-select");
    }
}

//推荐模块
var recommend = {
    //推荐栏目聚焦
    _focus: function (obj) {
        if (obj.ele.id.indexOf("recommend") > -1) {
            iptvFocus.prototype.addClass(obj.ele, "focus_btn");
        } else {
            iptvFocus.prototype.addClass(obj.ele, "recommend-m-focus");
        }
    },
    //推荐栏目失焦
    _blur: function (obj) {
        if (obj.ele.id.indexOf("recommend") > -1) {
            iptvFocus.prototype.removeClass(obj.ele, "focus_btn");
        } else {
            iptvFocus.prototype.removeClass(obj.ele, "recommend-m-focus");
        }
    },
    _enter: function (obj) {
        console.log(obj, "焦点记录已保存在cookie中");
    },
};
window.onload = function () {
    iptv = new iptvFocus({
        _group: "nav",
        focusClassScale: 1.2,
        visualMargin: 50,
        initNoFocus: true, //初始化不自动聚焦
        hasLayer: true, //禁止自动切换聚焦组
    });
    //获取并切换至上次浏览的栏目
    nav._enter(iptv.findFocusEle(iptv.cookie("home-nav") || 1));
    iptv.cookie(iptv.pathname) ? iptv.onFocus(iptv._nowEle) : iptv.focusGroup(nav._data[nav._navIndex])
    //移动到可视区域内
    iptv.moveScroll("right", iptv._nowEle);
    iptv.moveScroll("left", iptv._nowEle);
}
// 默认全局返回键监听回调
function BackParent() {
    console.log("返回/退出上级页面");
}
//监听nav导航聚焦完成回调
function onNavFocus(eleObj) {
    console.log(arguments, "---onNavFocus");
    nav._enter(eleObj);
}
window.onunload = function () {
    //离开页面时 自动记录焦点
    //iptv.saveFocusIndex();
    //保存当前浏览的栏目索引
    iptv.cookie("home-nav", nav._navIndex.toString());
};